﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SmartSpace.Silverlight
{
    public class FadeSwitchEffect : SwitchEffect
    {
        private Storyboard _storyBoard;

        private TimeSpan _duration;

        public FadeSwitchEffect()
        {
            _duration = TimeSpan.FromMilliseconds(500);
        }

        public FadeSwitchEffect(TimeSpan duration)
        {
            _duration = duration;
        }

        /// <summary>
        /// 准备切换工作
        /// </summary>
        /// <param name="containerSize"></param>
        /// <param name="outView"></param>
        /// <param name="inView"></param>
        /// <param name="direction"></param>
        protected override void OnPrepareSwitch(Size containerSize, SlView outView, SlView inView, SwitchDirection direction)
        {
            _storyBoard = new Storyboard();
            _storyBoard.Completed += new EventHandler(_storyBoard_Completed);

            if (Direction == SwitchDirection.Forward) // outView不透明，inView由透明变不透明
            {
                InView.Opacity = 0.0f;
                if (OutView != null)
                {
                    OutView.Opacity = 1.0f;
                }

                DoubleAnimation animationIn = new DoubleAnimation();
                animationIn.To = 1.0f;
                animationIn.Duration = _duration;
                animationIn.RepeatBehavior = new RepeatBehavior(1);
                animationIn.AutoReverse = false;
                Storyboard.SetTarget(animationIn, this.InView);
                Storyboard.SetTargetProperty(animationIn, new PropertyPath("Opacity"));
                _storyBoard.Children.Add(animationIn);
            }
            else if (Direction == SwitchDirection.Backward) // inView不透明，outView由不透明变透明
            {
                InView.Opacity = 1.0f;
                if (OutView != null)
                {
                    OutView.Opacity = 1.0f;

                    DoubleAnimation animationOut = new DoubleAnimation();
                    animationOut.To = 0;
                    animationOut.Duration = _duration;
                    animationOut.RepeatBehavior = new RepeatBehavior(1);
                    animationOut.AutoReverse = false;
                    Storyboard.SetTarget(animationOut, this.OutView);
                    Storyboard.SetTargetProperty(animationOut, new PropertyPath("Value"));
                    _storyBoard.Children.Add(animationOut);
                }
            }
        }

        void _storyBoard_Completed(object sender, EventArgs e)
        {
            FinishSwitch();
        }

        protected override void OnBegin()
        {
            _storyBoard.Begin();
        }
    }
}
